﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Client.Commands
{
    abstract class ACommand : ICommand
    {
        private static LinkedList<ICommand> commands = null;

        /// <summary>
        /// 获取所有命令
        /// </summary>
        /// <returns></returns>
        public static LinkedList<ICommand> Commands()
        {
            if (commands == null)
            {
                commands = new LinkedList<ICommand>();
                var types = TypeUtils.GetDefaultConstructorAssignableTypes<ICommand>(Assembly.GetExecutingAssembly(), null, true);
                foreach (var type in types)
                {
                    ICommand command = (ICommand)Activator.CreateInstance(type);
                    commands.AddLast(command);
                }
                return commands;
            }
            return commands;
        }

        /// <summary>
        /// 
        /// </summary>
        public abstract string CommandCode { get; }

        /// <summary>
        /// 
        /// </summary>
        public abstract string CommandHelpExplain { get; }

        bool ICommand.SendCommand(string args)
        {
            if (args.Equals(CommandCode, StringComparison.InvariantCultureIgnoreCase))
            {
                SendCommand("");
                return true;
            }
            if (args.StartsWith(CommandCode + " ", StringComparison.InvariantCultureIgnoreCase))
            {
                args = args.Substring(CommandCode.Length);
                SendCommand(args.Trim());
                return true;
            }
            return false;
        }

        public abstract void SendCommand(string args);
    }
}
